package com.ffox.service.impl;

import com.ffox.entity.UserInformation;
import com.ffox.fastDfsUtil.FastDfsUtil;
import com.ffox.mapper.UserLoginMapper;
import com.ffox.result.Result;
import com.ffox.service.UserLoginService;
import com.ffox.jjwt.JwtUtils;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.time.LocalDateTime;
import java.util.*;

/**
 * 用户登录服务实现类
 */
@Service
public class UserLoginServiceImpl implements UserLoginService {
    @Autowired
    private UserLoginMapper userLoginMapper;
    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;
    @Autowired
    private FastDfsUtil fastDfsUtil;

    /**
     * 用户登录方法
     *
     * @param number   用户编号
     * @param password 用户密码
     * @return 登录结果，包含用户信息
     */
    @Override
    public Result<Map<String, Object>> userLogin(String number, String password) {
//        根据number 查询用户所有信息
        UserInformation datas = userLoginMapper.getUserByNumber(number);
        if (datas == null) {
            return Result.fail("用户不存在");
        }
        if (!bCryptPasswordEncoder.matches(password, datas.getPassword())) {
            return Result.fail("用户名或密码错误");
        }
//        更新登录时间
        userLoginMapper.updateUser(datas.getId(), new HashMap<String, Object>() {{
            put("lastLoginTime", LocalDateTime.now());
        }});

        List<String> roles = Arrays.asList("ROLE_USER");

        // 生成JWT令牌
        String token = JwtUtils.generateToken(number,roles);

        Map<String, Object> userInfo = new HashMap<>();
        userInfo.put("name", datas.getNickName());
        userInfo.put("avatar", datas.getAvatar());
        userInfo.put("role", "user");
        userInfo.put("number", number);


        Map<String, Object> result = new HashMap<>();
        result.put("userInfo", userInfo);
        result.put("token", token);


        return Result.success("登录成功", result);
    }

    /**
     * 用户注册方法
     *
     * @param number   用户编号
     * @param password 用户密码
     * @param realName 真实姓名
     * @param idCard   身份证号
     * @param mobile   手机号
     * @param email    邮箱
     * @return 注册结果，包含影响行数
     */
    @Override
    public Result<Integer> userInsert(String number, String password, String realName, String idCard, String mobile, String email) {
        if (userLoginMapper.getUserByNumber(number) != null) {
            return Result.fail("用户已存在");
        }

        UserInformation loginUser = new UserInformation();
        loginUser.setNumber(number);
        loginUser.setPassword(bCryptPasswordEncoder.encode(password));
        loginUser.setRealName(realName);
        loginUser.setIdCard(idCard);
        loginUser.setMobile(mobile);
        loginUser.setEmail(email);
        loginUser.setNickName(number);
        loginUser.setAvatar("http://192.168.190.53:80/group1/M00/00/00/wKi-NWiYwqmARlxuAAEikS43N48254.png");
        loginUser.setCreatedAt(LocalDateTime.now());
        Integer result = userLoginMapper.insertUser(loginUser);
        if (result == 1) {
            return Result.success("用户添加成功", result);
        } else {
            return Result.fail("用户添加失败");
        }
    }

    /**
     * 用户删除方法
     *
     * @param id 用户ID
     * @return 删除结果，包含影响行数
     */
    @Override
    public Result<Integer> userDelete(Long id) {
        Integer result = userLoginMapper.updateUser(id, new HashMap<String, Object>() {{
            put("status", 0);
        }});
        if (result == 1) {
            return Result.success("用户删除成功", result);
        } else {
            return Result.fail("用户删除失败");
        }
    }
}
